<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
use classlib\validator\auditor;
class Crtmp extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$area  = \src\logic\lconfig::dd('system_rtmp_area');
		$type  = \src\logic\lconfig::dd('system_rtmp_types');
		$dlang = C\lang::inst('dd');
		$view->assign('area', $area);
		$view->assign('type', $type);
		$view->assign('status', $dlang->get('sh0006'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/rtmp.ext.js');
	}
	
	public function topsrc() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array('pid'=>0, 'stype'=>5);
		$fields = 'id,host as name';
		$orm    = C\model::load('pro.inc.rtmp');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total > 0) {
			$list = $orm->get_list($start, $limit, $where, $fields, $sort);
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	public function doarea() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array();
		$area   = input::get_post('area');
		if ($area) {
			$where['area'] = array('%'.$area.'%', orm::OP_LIKE);
		}
		$region = input::get_post('region');
		if (is_numeric($region)) {
			$where['region'] = $region;
		}
		$orm    = C\model::load('pro.inc.region');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total > 0) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$item['stime']= date('Y-m-d H:i:s', $item['stime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	/**
	 * @desc    删除地理位置机房分配
	 * @doc     删除记录
	 * @access  需要授权
	 */
	public function ardelete() {
		$view = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids  = auditor::intstr2arr('ids');
		$orm  = C\model::load('pro.inc.region');
		foreach ($ids as $id) {
			$data = $orm->get_one($id);
			if ($data) {
				Cadmin::log2db(json_encode($data));
				$orm->delete($id);
			}
		}
		$view->display();
	}
	
	/**
	 * @desc    保存地理位置机房分配
	 * @doc     保存记录
	 * @access  需要授权
	 */
	public function arsave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$filter = new \classlib\validator\auditor();
		$lang   = C\lang::inst('adm');
		$filter->lang($lang);
		$data   = array();
		try {
			$data['region']   = $filter->reset('region')->numeric()->min(0)->run();
			$data['area']     = $filter->reset('area')->required()->lbetween(1, 15)->run();
			$data['name']     = $filter->reset('name')->required()->lbetween(1, 63)->run();
			$data['area']     = strtoupper($data['area']);
			$data['area2int'] = input::strhash($data['area']);
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		$id    = (int)input::get_post('id');
		$orm   = C\model::load('pro.inc.region');
		$where = array('area2int'=>$data['area2int'], 'area'=>$data['area']);
		$item  = $orm->get_item($where);
		if ($item && $item['id'] != $id) {
			$view->assign_error(40028, $lang->common('40028'));
		}
		$data['stime'] = C\config::$nowtime;
		($id > 0)? $orm->save($id, $data):$orm->new_one($data);
		Cadmin::log2db(C\view::inst('xml')->array2xml($data));
		$view->display();
	}
	
	/**
	 * @desc    保存RTMP资料信息
	 * @doc     编辑/添加地址
	 * @access  需要授权 
	 */
	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$filter = new \classlib\validator\auditor();
		$lang   = C\lang::inst('adm');
		$filter->lang($lang);
		$data   = array();
		try {
			$data['pid']    = (int)input::get_post('pid');
			$data['rank']   = $filter->reset('rank')->numeric()->min()->run();
			$data['limitfd']= $filter->reset('limitfd')->numeric()->min(0)->run();
			$data['status'] = $filter->reset('status')->numeric()->min(0)->run();
			$data['stype']  = $filter->reset('stype')->numeric()->min(0)->run();
			$data['region'] = $filter->reset('region')->numeric()->min(0)->run();
			$data['rtmp']   = $filter->reset('rtmp')->required()->lbetween(1, 255)->run();
			$data['host']   = $filter->reset('host')->required()->lbetween(6, 63)->run();
			$data['ports']  = $filter->reset('ports')->required()->lbetween(1, 255)->run();
			$ports = auditor::intstr2arr('ports');
			$data['ports']  = implode(',', $ports);
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		$id    = (int)input::get_post('id');
		$orm   = C\model::load('pro.inc.rtmp');
		$data['stime'] = C\config::$nowtime;
		($id > 0)? $orm->save($id, $data):$orm->new_one($data);
		Cadmin::log2db(C\view::inst('xml')->array2xml($data));
		$view->display();
	}
	
	/**
	 * @desc    删除RTMP地址配置
	 * @doc     删除记录
	 * @access  需要授权
	 */
	public function dodelete() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids    = auditor::intstr2arr('ids');
		$orm    = C\model::load('pro.inc.rtmp');
		foreach ($ids as $id) {
			$data = $orm->get_one($id);
			if ($data) {
				Cadmin::log2db(C\view::inst('xml')->array2xml($data));
				$orm->delete($id);
			}
		}
		$view->display();
	}
	
	/**
	 * 获取资料列表
	 */
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$orm    = C\model::load('pro.inc.rtmp')->get_orm();
		$rtmp   = input::get_post('rtmp');
		if ($rtmp) {
			$orm->where('rtmp', '%'.$rtmp.'%', \classlib\orm::OP_LIKE);
		}
		$region = (int)input::get_post('region');
		if ($region > 0) {
			$orm->where('region', $region);
		}
		$total  = $orm->field('count(1)')->get_value();
		$list   = array();
		if ($total > 0) {
			$list = $orm->clear('field')->field('*')->get_list(NULL, $start, $limit);
			$orm  = C\model::load('pro.inc.rtmp');
			foreach ($list as &$item) {
				$item['stime']= date('Y-m-d H:i:s', $item['stime']);
				$item['phost']= '';
				if ($item['pid'] > 0) {
					$data = $orm->get_one($item['pid']);
					$item['phost'] = $data['host'];
				}
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}